package com.gfxy.modules.items.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gfxy.modules.common.entity.MemberList;
import com.gfxy.modules.items.entity.Jxms;

import com.gfxy.modules.items.entity.teachingresult.TeachingResultContent;
import com.gfxy.modules.items.mapper.JxmsMapper;
import com.gfxy.modules.items.service.IJxmsService;
import com.gfxy.modules.items.vo.JxmsPage;
import com.gfxy.modules.excel.ExcelUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.ImportExcelUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.io.PushbackInputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 教学名师
 * @Author: jeecg-boot
 * @Date:   2021-08-19
 * @Version: V1.0
 */
@Service
public class JxmsServiceImpl extends AbstractProjectServiceImpl<JxmsMapper, Jxms> implements IJxmsService {

	protected int memberCellNum = 2;

	@Override
	protected void pass(String id, String isPass) {

	}

	@Override
	protected String[] getMemberArray(Row row) {
		String nameStrs = ExcelUtil.formatCell(row.getCell(memberCellNum));
		return nameStrs.split(memberNameSeparator);
	}

	@Override
	protected Jxms getOneProject(Row row) {
		String classessStr = ExcelUtil.formatCell(row.getCell(1));
		String date = ExcelUtil.formatCell(row.getCell(0));

		Jxms jxms = new Jxms();
		jxms.setJxmsClassess(translateClassess(classessStr));
		jxms.setDate(date);
		jxms.setProjectName(classessStr+"教学名师");

		jxms.setTotalScore(this.getTotalScore(jxms));

		return jxms;
	}

	@Override
	public Double getTotalScore(Jxms jxms) {
		int classess = jxms.getJxmsClassess();
		double result = 0.0;
		if(classess==3){
			result=15;
		}else if (classess==2){
			result=80;
		}else if (classess==1){
			result=200;
		}
		return result;
	}


	@Override
	public IPage<JxmsPage> jxmsPageListByYear(String date, Integer pageNo, Integer pageSize) {
		Page<JxmsPage> page = new Page<>(pageNo,pageSize);
		return page.setRecords(this.baseMapper.jxmsVoListByYear(date));
	}

	@Override
	public List<JxmsPage> jxmsPageListByYear(String date) {
		return this.baseMapper.jxmsVoListByYear(date);
	}

	private Integer translateClassess(String name){
		if(name == null || name.equals("")){
			return 0;
		} else if(name.equals("国家级")){
			return 1;
		} else if(name.equals("省级")){
			return 2;
		} else if(name.equals("校级")){
			return 3;
		} else {
			return 0;
		}
	}

	@Override
	protected List<TeachingResultContent> createTeachingResultContent(Jxms jxcg)  {

		List<TeachingResultContent> result = new ArrayList<>();

		// 类型
		TeachingResultContent classTeachingResultContent = new TeachingResultContent();
		classTeachingResultContent.setType(1);
		classTeachingResultContent.setLevel(jxcg.getJxmsClassess());

		try {
			classTeachingResultContent.setValue(dictTranslater.getDictValue(JxmsPage.class, "jxmsClassess", String.valueOf(jxcg.getJxmsClassess())));
			result.add(classTeachingResultContent);
		} catch (NoSuchFieldException e) {
			log.error(e.getMessage(),e);
		}
		return result;
	}

	@Override
	public Result<?> importExcel(MultipartFile file) throws IOException {
		int SHEET_NUM = 0;
		int errorLines = 0;
		int successLines = 0;
		InputStream inputStream = file.getInputStream();
		if (!(inputStream).markSupported()) {
			inputStream = new PushbackInputStream(inputStream, 8);
		}
		Workbook book = WorkbookFactory.create(inputStream);
		Sheet sheet = book.getSheetAt(SHEET_NUM);
		List<String> errorMessage = new ArrayList<>();

		for (int i = START_ROW_NUM; i <= sheet.getLastRowNum(); i++) {
			ExcelResult excelResult = new ExcelResult(true,"");
			try{

				Row row = sheet.getRow(i);
				Jxms t = this.getOneProject(row);

				List<MemberList> memberListList = this.getMemberList(row, t, excelResult);

				for (MemberList memberList : memberListList) {
					List<MemberList> memberLists = new ArrayList<>();
					memberList.setRanked(1);
					memberList.setScore(t.getTotalScore());
					memberLists.add(memberList);
					this.saveMain(t,memberLists);
					t.setId(null);
				}
			} catch ( Exception e){
				e.printStackTrace();
				log.error(e.getMessage(),e);
				excelResult.setErrorMsg("excel 第 " + i+ "行数据解析出现位置异常; ");
			}

			if(excelResult.isSuccess()){
				successLines++;
			} else {
				errorLines++;
				errorMessage.add(excelResult.getErrorMsg());
			}

		}

		return ImportExcelUtil.imporReturnRes(errorLines, successLines, errorMessage);
	}
}
